//
//  SCNTsukkomiViewController.swift
//  ShoucainuSwift
//
//  Created by 王家旭 on 2017/5/16.
//  Copyright © 2017年 Riluee. All rights reserved.
//

import UIKit

class SCNTsukkomiViewController: SCNTableViewController {

    lazy var textView: SCNTextView = {
       
        let view = SCNTextView()
        view.font = UIFont.systemFont(ofSize: 15.5)
        view.layer.cornerRadius = 5.0
        view.layer.borderColor = SCNConstants.colorWith255RGBA(230, g: 230, b: 230, a: 1).cgColor
        view.layer.borderWidth = 1.0
        view.placeText = "亲爱的财主大人，请把您的意见或建议提交给我们，我们会及时与您联系"
        return view
    }()
    
    lazy var typeL: UILabel = {
        let lab = UILabel()
        lab.textColor = SCNConstants.colorWith255RGBA(36, g: 36, b: 36, a: 1)
        lab.text = "联系方式"
        lab.font = UIFont.systemFont(ofSize: 17.0)
        return lab
    }()
    lazy var phoneB: UIButton = {
       

        let btn = SCNTypeButton()
        btn.setTitle("手机号码", for: .normal)
        btn.isSelected = true
        btn.tag = 11
        btn.addTarget(self, action: #selector(typeAction(_ :)), for: .touchUpInside)
        return btn
    }()
    lazy var QQNumberB: SCNTypeButton = {
        
        let btn = SCNTypeButton()
        btn.tag = 12
        btn.setTitle("QQ号码", for: .normal)
        btn.addTarget(self, action: #selector(typeAction(_ :)), for: .touchUpInside)
        return btn
    }()
    lazy var textfield: UITextField = {
       
        let field = UITextField()
        field.borderStyle = .roundedRect
        field.placeholder = "请输入号码"
        return field
    }()
    lazy var commitB: UIButton = {
       
        let btn = UIButton(type: .custom)
        btn.backgroundColor = SCNConstants.rootBackgroundColor
        btn.setTitle("提交", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.layer.cornerRadius = 5.0
        btn.layer.masksToBounds = true
        btn.addTarget(self, action: #selector(commitAction(_ :)), for: .touchUpInside)
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "我要吐槽"
        
        self.automaticallyAdjustsScrollViewInsets = false
        
        self.view.addSubview(textView)
        self.view.addSubview(typeL)
        self.view.addSubview(phoneB)
        self.view.addSubview(QQNumberB)
        self.view.addSubview(textfield)
        self.view.addSubview(commitB)

        layout()
    }

    func layout() {
        
        
        textView.snp.makeConstraints { (make) in
            make.top.equalTo(80)
            make.leading.equalTo(10)
            make.trailing.equalTo(-10)
            make.height.equalTo(200)
        }
        typeL.snp.makeConstraints { (make) in
            make.leading.equalTo(10)
            make.top.equalTo(textView.snp.bottom).offset(27)
            make.size.equalTo(CGSize(width: 70, height: 20))
        }
        phoneB.snp.makeConstraints { (make) in
            make.leading.equalTo(typeL.snp.trailing).offset(10)
            make.top.equalTo(textView.snp.bottom).offset(20)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        QQNumberB.snp.makeConstraints { (make) in
            make.leading.equalTo(phoneB.snp.trailing).offset(10)
            make.top.equalTo(textView.snp.bottom).offset(20)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        textfield.snp.makeConstraints { (make) in
            make.leading.equalTo(typeL.snp.trailing).offset(10)
            make.top.equalTo(phoneB.snp.bottom).offset(15)
            make.size.equalTo(CGSize(width: 210, height: 35))
        }
        commitB.snp.makeConstraints { (make) in
            make.leading.equalTo(20)
            make.top.equalTo(textfield.snp.bottom).offset(30)
            make.size.equalTo(CGSize(width: SCNConstants.SCREENWIDTH - 40, height: 35))
        }
    }
    
    
}


extension SCNTsukkomiViewController {
    
    // MARK: Action
    
    func typeAction(_ sender: SCNTypeButton) {
        phoneB.isSelected = false
        QQNumberB.isSelected = false
        sender.isSelected = true
    }
    func commitAction(_ sender: UIButton) {
        /*
         参数为：
         "relation_type" 号码类型：1为手机/0为QQ
         "content" 反馈内容
         "relation_numb" 号码
         限制一天可提交10次
         */
        let relationType = phoneB.isSelected ? "1":"0"
        
        let param = [
            "relation_type" : relationType,
            "content" : String(format: "%@", textView.text),
            "relation_numb" : String(format: "%@", textfield.text!)
        ]
        SCNRequestManager.postRequest(param, URL: SCNRequestAPI.menber().reedBack) { (result) in
            let value = result.result.value as? Dictionary<String, Any>
            let dataresult = value?["dataresult"] as? Dictionary<String, Any>
            let msg = dataresult?["sucmsg"] as? String
            if let tmp = msg {
                self.noticeOnlyText(tmp)
            }
            
        }
    }
}
